package logic

import (
	"context"
	"time"

	"mall/common/enumx"
	"mall/common/resultx"
	"mall/service/blog/model"
	"mall/service/blog/rpc/internal/svc"
	"mall/service/blog/rpc/types/blog"

	"github.com/zeromicro/go-zero/core/logx"
)

type CreateProposalLogic struct {
	ctx    context.Context
	svcCtx *svc.ServiceContext
	logx.Logger
}

func NewCreateProposalLogic(ctx context.Context, svcCtx *svc.ServiceContext) *CreateProposalLogic {
	return &CreateProposalLogic{
		ctx:    ctx,
		svcCtx: svcCtx,
		Logger: logx.WithContext(ctx),
	}
}

func (l *CreateProposalLogic) CreateProposal(in *blog.CreateProposalReq) (*blog.CreateProposalResp, error) {
	if in.ParentId == 0 {
		return nil, resultx.StatusError(resultx.ENTRY_PERFUME_ID_REQUIRED, "")
	}

	if in.AuthorId == 0 {
		return nil, resultx.StatusError(resultx.USER_GET_INFO_FAILED, "")
	}

	if in.Content == "" {
		return nil, resultx.StatusError(resultx.ENTRY_PROPOSAL_CONTENT_REQUIRED, "")
	}

	now := time.Now()
	var proposal model.BlogKnowledgeBaseProposal
	proposal.PostAuthorID       = uint64(in.AuthorId)
	proposal.PostAuthorNickname = in.AuthorNickname
	proposal.PostAuthorAvatar   = in.AuthorAvatar
	proposal.PostAuthorLevel    = uint8(in.AuthorLevel)
	proposal.PostParentID       = uint64(in.ParentId)
	proposal.PostContent        = in.Content
	proposal.Perfumers          = in.Perfumer
	proposal.FragranceFamilies  = in.FragranceFamily
	proposal.TopNotes           = in.TopNotes
	proposal.HeartNotes         = in.HeartNotes
	proposal.BaseNotes          = in.BaseNotes
	proposal.Year               = in.Year
	proposal.PostStatus         = uint16(enumx.ObjectStatusPending)
	proposal.DelStatus          = uint16(enumx.ObjectStatusPending)
	proposal.PostDate           = now
	proposal.PostModifiedDate   = now

	err := l.svcCtx.BlogUser.InsertProposal(l.ctx, &proposal)
	if err != nil {
		return nil, err
	}

	return &blog.CreateProposalResp{}, nil
}
